home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-valint.adb < prev    next >
Text File  |  1996-01-30  |  3KB  |  92 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                       S Y S T E M . V A L _ I N T                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.6 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Unsigned_Types; use System.Unsigned_Types;
  27. with System.Val_Uns;        use System.Val_Uns;
  28. with System.Val_Util;       use System.Val_Util;
  29.  
  30. package body System.Val_Int is
  31.  
  32.    ------------------
  33.    -- Scan_Integer --
  34.    ------------------
  35.  
  36.    function Scan_Integer
  37.      (Str  : String;
  38.       Ptr  : access Positive'Base;
  39.       Max  : Positive'Base)
  40.       return Integer
  41.    is
  42.       Uval : Unsigned;
  43.       --  Unsigned result
  44.  
  45.       Minus : Boolean := False;
  46.       --  Set to True if minus sign is present, otherwise to False
  47.  
  48.       Start : Positive;
  49.       --  Saves location of first non-blank (not used in this case)
  50.  
  51.    begin
  52.       Scan_Sign (Str, Ptr, Max, Minus, Start);
  53.       Uval := Scan_Unsigned (Str, Ptr, Max);
  54.  
  55.       --  Deal with overflow cases, and also with maximum negative number
  56.  
  57.       if Uval > Unsigned (Integer'Last) then
  58.          if Minus and then Uval = Unsigned (-(Integer'First)) then
  59.             return Integer'First;
  60.          else
  61.             raise Constraint_Error;
  62.          end if;
  63.  
  64.       --  Negative values
  65.  
  66.       elsif Minus then
  67.          return -(Integer (Uval));
  68.  
  69.       --  Positive values
  70.  
  71.       else
  72.          return Integer (Uval);
  73.       end if;
  74.  
  75.    end Scan_Integer;
  76.  
  77.    -------------------
  78.    -- Value_Integer --
  79.    -------------------
  80.  
  81.    function Value_Integer (Str : String) return Integer is
  82.       V : Integer;
  83.       P : aliased Natural := Str'First;
  84.  
  85.    begin
  86.       V := Scan_Integer (Str, P'Access, Str'Last);
  87.       Scan_Trailing_Blanks (Str, P);
  88.       return V;
  89.    end Value_Integer;
  90.  
  91. end System.Val_Int;
  92.